package com.rparoxysm.tribase.util;

import java.util.Map;
import java.util.HashMap;

public class Debug {
    public static final int NONE  = 0;
    public static final int ERROR = 1;
    public static final int WARN  = 3;
    public static final int DATA  = 5;
    public static final int ENTRY = 7;
    public static final int INFO  = 9;
    public static final int ALL   = 10;
    
    private static Map<String, Integer> levels = new HashMap<String, Integer>();
    private static final int DEFAULT_DEBUG_LEVEL = INFO;
    private static Debug debug = new Debug("Debug");

    /**
     * Set the debug level for a scope to a given value.
     *
     * If the scope does not exist in the map, create it.
     */
    public synchronized static void setLevel(String name, int level) {
	if(debug != null)
	    debug.print(5, "Setting " + name + " debug level to " + level);

	levels.put(name, level);
    }
    
    public synchronized static int getLevel(String name) {
	Integer debugLevel = levels.get(name);
	return (debugLevel == null) ? DEFAULT_DEBUG_LEVEL : debugLevel;
    }

    private String debugScope;

    public Debug(String scope) {
	debugScope = new String(scope);
	
	// obtain the default debug level for this object
	int level = getLevel(debugScope);
	setLevel(debugScope, level);
    }
    
    public void print(int level, String text) {
	if(level <= getLevel(debugScope)) {
	    System.out.println("[" + debugScope + "," + level + "]: " + text);
	}
    }

    public static void main(String args[]) {
	Debug one   = new Debug("one");
	Debug two   = new Debug("two");
	Debug three = new Debug("three");;
	
	one.print(3,   "1 - This should be seen");
	two.print(7,   "This should NOT be seen");
	three.print(5, "2 - This should be seen");
	Debug.setLevel("one", 2);

	one.print(3,   "This should NOT be seen");
	two.print(7,   "This should NOT be seen");
	three.print(5, "3 - This should be seen");
    }
}
